package s.k.gui.webtest.basetools.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

public class MyDirectoryOperation {

	private String absolutePath;

	public MyDirectoryOperation(String absolutePath) {

		this.absolutePath = absolutePath;
	}

	public ArrayList<File> getChildFiles() {

		ArrayList<File> childFiles = new ArrayList<File>();

		for (File file : new File(absolutePath).listFiles()) {

			if (file.isFile())

				childFiles.add(file);

		}
		return childFiles;

	}

	public ArrayList<File> getDirectorys() {

		ArrayList<File> childFolders = new ArrayList<File>();

		for (File file : new File(absolutePath).listFiles()) {

			if (file.isDirectory())

				childFolders.add(file);
		}

		return childFolders;
	}

	public String getAbsolutePath() {

		return absolutePath;
	}

	/**
	 * �ݹ����copy
	 * 
	 * @param sourceDir
	 *            Ŀ����Ŀ¼
	 * @param destinationDir
	 *            Ҫ�������ĵط���Ҳ��һ��Ŀ¼����Ŀ¼����ΪĿ�꿽��Ŀ¼�ĸ�Ŀ¼
	 * @throws IOException
	 * */
	public static void copyTo(File destination, File source) {

		if (!destination.exists()) {

			destination.mkdir();
		}
		for (File file : source.listFiles()) {

			if (file.isFile()) {

				copyFile(file, new File(destination.getAbsolutePath() + "\\"
						+ file.getName()));

			} else {

				copyTo(new File(destination.getAbsolutePath() + "\\"
						+ file.getName()), file);
			}
		}
	}

	/**
	 * �����ļ��Ĺ�����
	 * 
	 * @param sourceFile
	 *            Դ�ļ�
	 * @param destinationFile
	 *            Ŀ���ļ������������ļ�
	 * */
	private static void copyFile(File sourceFile, File destinationFile) {

		try {

			if (sourceFile.isDirectory())

				return;

			FileInputStream finput = new FileInputStream(sourceFile);

			if (!destinationFile.exists())

				destinationFile.createNewFile();

			FileOutputStream foutput = new FileOutputStream(destinationFile);

			byte[] buffered = new byte[1024];

			while (finput.read(buffered) != -1) {

				foutput.write(buffered);

			}
			finput.close();

			foutput.close();

		} catch (FileNotFoundException e) {

			e.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();

		}
	}
}
